package com.os.opencv.java.chapter12;

import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

public class FindFace {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        //读取图像并在屏幕上显示
        Mat src = Imgcodecs.imread("pics/team.png");
        HighGui.imshow("src", src);
        HighGui.waitKey(0);

        //创建级联分类器并进行检测
        CascadeClassifier detector = new CascadeClassifier("haarcascades/haarcascade_frontalface_default.xml");
        MatOfRect detectResult = new MatOfRect();  //用于存放检测结果
        detector.detectMultiScale(src, detectResult, 1.2, 3, 0, new Size(30,30), new Size(200,200));

        //如检测到为止结果，则直接返回
        if(detectResult.toArray().length <= 0){
            System.out.println("no face detected");
            return;
        }

        //如检测到结果，则用矩形画出
        for(Rect rect : detectResult.toArray()){
            Imgproc.rectangle(src, new Point(rect.x, rect.y), new Point(rect.x+rect.width, rect.y+rect.height), new Scalar(255,0,0), 2);
        }

        //在屏幕上显示检测结果
        HighGui.imshow("face", src);
        HighGui.waitKey(0);
        System.exit(0);
    }
}
